package com.pug.aspect;

import com.pug.dto.MessageDto;
import com.pug.service.message.MessageService;
import com.pug.vo.Order;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Aspect
@Component
public class SaveOrderAspect {
    @Resource
    MessageService messageService;
    public SaveOrderAspect(){};
    //切点
    @Pointcut("@annotation(com.pug.annotation.SaveOrderMessage)")
    public void saveOrderPointcut(){
    }
    //保存订单消息
    @After(value = "saveOrderPointcut()")
    public void saveOrder(JoinPoint joinPoint){
        // 发送消息
        Object[] args = joinPoint.getArgs();
        Order order=(Order)args[0];
        MessageDto messageMo = new MessageDto();
        messageMo.setTitle("订单通知");
        messageMo.setMsgcontent("用户【"+order.getNickname()+"】您好，" +
                "你已经成功入住【xxx酒店】 \n 酒店的地址:xxxx \n联系方式是：158787xxxx。");
        messageMo.setMsgtype(2);
        messageMo.setCreateTime(new Date());
        messageMo.setNickname(order.getNickname());
        messageMo.setUserid(order.getUserId()+"");
        messageMo.setAvatar(order.getAvatar());
        messageMo.setIcon("/static/icon-item-002.png");
        messageService.saveMessage(messageMo);
    }
}
